<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>授课管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="pageQuery" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" clearable v-model="pageQuery.courseName" placeholder="课程名称"></el-input>
      </el-form-item>
      <el-form-item label="班级名称">
        <el-input size="small" clearable v-model="pageQuery.className" placeholder="班级名称"></el-input>
      </el-form-item>
      <el-form-item label="教师名称">
        <el-input size="small" clearable v-model="pageQuery.teacherName	" placeholder="教师名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-button type="warning" @click="addTeaching" style="margin-bottom: 20px">添加授课</el-button>
    <el-table size="small" :data="teachingList" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" prop="id" label="授课编号" width="100">
      </el-table-column>
      <el-table-column align="center" prop="courseName" label="授课名称" width="200">
      </el-table-column>
      <el-table-column align="center" prop="teacherName" label="授课老师" width="150">
      </el-table-column>
      <el-table-column align="center" prop="className" label="授课班级" width="150">
      </el-table-column>
      <el-table-column align="center" prop="termDescribes" label="授课学期" width="200">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="editTeaching(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteTeaching(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageQuery" :total="total" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="editTitle" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="教师名称" prop="teacherId">
          <el-select v-model="editForm.teacherId" clearable placeholder="教师名称" @change="teacherChange">
            <el-option :label="item.name" :value="item.id" v-for="item in teacherList" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级名称" prop="classId">
          <el-select v-model="editForm.classId" clearable placeholder="班级名称" @change="classChange">
            <el-option :label="item.className" :value="item.id" v-for="item in classList" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程名称" prop="courseId">
          <el-select v-model="editForm.courseId" clearable placeholder="课程名称" @change="courseChange">
            <el-option :label="item.courseName" :value="item.id" v-for="item in courseList" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm(editOrAdd ? 'editForm' : 'addForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '../../components/Pagination'
export default {
  name:'TeachingDetail',
  data() {
    return {
      editOrAdd: true, // 编辑 / 新增 => true / false
      teachingList: [], //授课列表
      pageQuery: {
          courseName: '',
          className: '',
          teacherName: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      query: {
        pageNum: 1,
        pageSize: -1
      },
      loading: false, //是显示加载
      editFormVisible: false, // 编辑框是否弹出
      editTitle: '编辑授课',
      editForm: {
        id: '',
        courseId: '',
        courseName: '',
        teacherId: '',
        teacherName: '',
        classId: '',
        className: ''
      },
      // rules表单验证
      rules: {
        courseId: [
          { required: true, message: '请选择课程', trigger: 'change' }
        ],
        teacherId: [{ required: true, message: '请选择教师', trigger: 'change' }],
        classId: [{ required: true, message: '请选择班级', trigger: 'change' }]
      },
      courseList: [],
      classList: [],
      teacherList: []
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  created() {
    this.getTeachingList()
    this.getCourseList()
    this.getClassList()
    this.getTeacherList()
  },
  methods: {
      teacherChange (e) {
          this.editForm.teacherName = this.teacherList.filter(item => item.id == e)[0].name
      },
      classChange (e) {
          this.editForm.className = this.classList.filter(item => item.id == e)[0].className
      },
      courseChange (e) {
          this.editForm.courseName = this.courseList.filter(item => item.id == e)[0].courseName
      },
    /**
     * 搜索
     */
    search () {
      this.getTeachingList()
    },
    /**
     * 获取课程列表
     */
    getCourseList() {
    //   this.loading = true
      this.$api.course.getCourseList(this.query).then(res => {
        if (res.code === 200) {
          this.courseList = res.data.records
        //   this.total = res.data.total
        //   this.loading = false
        }
      }).catch(err => this.loading = false)
    },
    /**
     * 获取班级列表
     */
    getClassList() {
    //   this.loading = true
      this.$api.course.getClassList(this.query).then(res => {
        if (res.code === 200) {
          this.classList = res.data.records
        //   this.total = res.data.total
        //   this.loading = false
        }
      }).catch(err => this.loading = false)
    },
    /**
     * 获取教师列表
     */
    getTeacherList() {
    //   this.loading = true
      this.$api.manager.getTeacherList(this.query).then(res => {
        if (res.code === 200) {
          this.teacherList = res.data.records
        //    this.total = res.data.total
        //   this.loading = false
        }
      }).catch(err => this.loading = false)
    },
    /**
     * 编辑弹框保存授课修改信息
     */
    submitForm (msg) {
      if (msg === 'editForm') {
        this.$api.course.discTeaching(this.editForm).then(res => {
          if (res.code === 200) {
            this.closeDialog()
            this.getTeachingList()
          }
        }).catch(err => this.editFormVisible = false)
      } else {
        delete this.editForm.id
        this.$api.course.addTeaching(this.editForm).then(res => {
          if (res.code === 200) {
            this.closeDialog()
            this.getTeachingList()
          }
        })
      }
    },
    /**
     * 获取授课列表
     */
    getTeachingList() {
      this.loading = true
      this.$api.course.getTeachingList(this.pageQuery).then(res => {
        if (res.code === 200) {
          this.teachingList = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      }).catch(err => this.loading = false)
    },
    /**
     * 分页插件事件
     */
    callFather(parm) {
      this.pageQuery.pageNum = parm.currentPage
      this.pageQuery.pageSize = parm.pageSize
      this.getTeachingList()
    },
    /**
     * 关闭编辑、增加弹出框
     */
    closeDialog() {
      this.editFormVisible = false
      this.editForm.courseId = ''
      this.editForm.teacherId = ''
      this.editForm.classId = ''
    },
    /**
     * 编辑授课
     */
    editTeaching (teaching) {
      this.editOrAdd = true
      this.editFormVisible = true
      this.editTitle = "编辑授课"
      this.editForm.id = teaching.id
      this.editForm.courseId = teaching.courseId
      this.editForm.teacherId = teaching.teacherId
      this.editForm.classId = teaching.classId
    },
    /**
     * 增加授课
     */
    addTeaching () {
      this.editOrAdd = false
      this.editFormVisible = true
      this.editTitle = "添加授课"
      this.editForm.courseId = ''
      this.editForm.teacherId = ''
      this.editForm.classId = ''
    //   this.editForm.teacherId = ''
    },
    /**
     * 删除授课
     */
    deleteTeaching (index, teaching) {
      this.$api.course.deleteTeaching({id:teaching.id}).then(res => {
        if (res.code === 200) {
          this.getTeachingList()
        }
      })
    }
  }
}
</script>

<style>
.teaching-search {
  margin-top: 20px;
}
.teachingRole {
  width: 100%;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

 
 